package myp72;

import result.Result;
import tasks.Task;

public class TaskP72 extends Task {

	private int threadID;
	private int i;
	private int num_threads;
	private int ct;

	public TaskP72(int i, int ID, int threads) {
		super(ID);
		this.i = i;
		this.threadID = ID;
		this.num_threads = threads;

	}


	@Override
	public void run() {
		for (int d = threadID; d <= i; d = d + num_threads) {
			int a = (d + 1) % 2;
			for (int n = 1; n < d; n = n + 1 + a) {
				if (gcd(d, n) == 1) {
					ct++;
				}
			}
		}

	}

	@Override
	public Result getResult() {
		return new ResultP72(ct);
	}

	@Override
	public void cancel() {
		// TODO Auto-generated method stub

	}

    private final int gcd(final int a, final int b) {
        /* Eucledean Method to determine the gcd (greateast common denominator or highest common factor) */
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }

}
